package com.ys.controller.api.report;

import com.ys.entity.report.ReFinanceExamine;
import com.ys.entity.sys.YsCol;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/reFinanceExamine")
public class ApiInterReFinanceExamineController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterReFinanceExamineController.class);
    private static Long MENU_ID = 10154L;

    @Autowired
    private CommonApiService<ReFinanceExamine> commonApiService;


    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public ReFinanceExamine setSeachSql(ReFinanceExamine cond){
        //cond.addField("CASE WHEN A.ExamineStatus = 1 THEN '<span style=\"color:red;\">有误</span>' ELSE '无误' END examineStatusHtml ");
        cond.addField("CASE WHEN A.ExamineStatus = 1 THEN '有误' ELSE '无误' END examineStatusHtml ");
        //排序
        cond.setOrderby("A.ExamineTime DESC,A.AccountCode ASC");
        return  cond;
    }


    /**
     * 查询
     */
    @RequiresPermissions("reFinanceExamine:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute ReFinanceExamine cond
    ){
        try {
            cond = setSeachSql(cond);
            LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
            return tableVoJA;
        }catch (Exception e){
            logger.error("查询财务核账表出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }


    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute ReFinanceExamine cond){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(MENU_ID)
                    .where("A.Code != 'ope'")
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();
            //处理数据
            ExportExcelUtils<ReFinanceExamine> util = new ExportExcelUtils<>(new ReFinanceExamine());
            String toFilePath = util.exportExcel(listData, listField, "财务核账表");
            UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出财务核账表出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }


}